package org.dtrd.modules.doctor.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.config.system.Constant;
import org.dtrd.modules.doctor.entity.bean.UserBankDetail;
import org.dtrd.modules.doctor.entity.bean.UserBankListInfo;
import org.dtrd.modules.doctor.entity.po.DtrdEntRdDoctor;
import org.dtrd.modules.doctor.entity.po.DtrdEntRdUserBank;
import org.dtrd.modules.doctor.entity.request.UserBankListRequest;
import org.dtrd.modules.doctor.mapper.DtrdEntRdUserBankMapper;
import org.dtrd.modules.doctor.service.IDtrdEntRdDoctorService;
import org.dtrd.modules.doctor.service.IDtrdEntRdUserBankService;
import org.dtrd.common.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author QR
 * @since 2021-12-14
 */
@DS("multi-datasource1")
@Service
public class DtrdEntRdUserBankServiceImpl extends ServiceImpl<DtrdEntRdUserBankMapper, DtrdEntRdUserBank> implements IDtrdEntRdUserBankService {

    private DtrdEntRdUserBankMapper dtrdEntRdUserBankMapper;

    @Autowired
    private IDtrdEntRdDoctorService doctorService;

    @Override
    public List<UserBankListInfo> getUserBankList(UserBankListRequest request) {
        LambdaQueryWrapper<DtrdEntRdUserBank> queryWrapper = baseQueryWrapper();
        List<DtrdEntRdUserBank> poList = list(queryWrapper);
        List<UserBankListInfo> dtoList = new ArrayList<>(poList.size());
        if (!poList.isEmpty()) {
            dtoList = poList.stream().map(UserBankListInfo::new).collect(Collectors.toList());
        }
        return dtoList;
    }

    @Override
    public UserBankDetail getUserDefaultBank(Integer doctorId) {
        LambdaQueryWrapper<DtrdEntRdUserBank> queryWrapper = baseQueryWrapper()
                .eq(DtrdEntRdUserBank::getUserBankUserId, doctorId)
                .eq(DtrdEntRdUserBank::getUserBankIsDefault, Constant.BankCardStatus.DEFAULT.getType());
        DtrdEntRdUserBank dtrdEntRdUserBank = getOne(queryWrapper);
        return Optional.ofNullable(dtrdEntRdUserBank)
                        .map(po -> new UserBankDetail().parseFromPo(po))
                        .orElse(null);
    }

    @Override
    public boolean saveOrUpdateUserBank(UserBankDetail dto) {
        Integer doctorId = dto.getUserBankUserId();
        DtrdEntRdUserBank dtrdEntRdUserBank = new DtrdEntRdUserBank();
        // 新增的时候将新增的银行卡设置成用户唯一的默认银行卡
        if (dto.getDataId() == null) {
            LambdaQueryWrapper<DtrdEntRdUserBank> bankQuery = baseQueryWrapper()
                    .eq(DtrdEntRdUserBank::getUserBankUserId, doctorId)
                    .eq(DtrdEntRdUserBank::getUserBankIsDefault, 1);
            List<DtrdEntRdUserBank> userBanks = this.list(bankQuery);
            for (DtrdEntRdUserBank userBank : userBanks) {
                userBank.setUserBankIsDefault(0);
            }
            updateBatchById(userBanks);
            // 新增的银行卡设置为默认银行卡
            dtrdEntRdUserBank = dtrdEntRdUserBank.init();
            dtrdEntRdUserBank.setUserBankIsDefault(1);
        }
        dtrdEntRdUserBank.parseFromDto(dto);
        // 用户类型，此处为医生
        dtrdEntRdUserBank.setUserBankUserType(Constant.BankUserType.DOCTOR.getType());
        // 设置密码
        DtrdEntRdDoctor queryDoctor = doctorService.getById(doctorId);
        if (StrUtil.isBlank(queryDoctor.getDoctorsWalletPassword()) && StrUtil.isNotBlank(dto.getWithDrawPwd())) {
            String pwd = StringUtil.Md5(dto.getWithDrawPwd());
            queryDoctor.setDoctorsWalletPassword(pwd);
            doctorService.updateById(queryDoctor);
        }
        return saveOrUpdate(dtrdEntRdUserBank);
    }

    @Override
    public boolean removeUserBank(Integer dataId) {
        return removeById(dataId);
    }

    private LambdaQueryWrapper<DtrdEntRdUserBank> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdEntRdUserBank.class)
                        .eq(DtrdEntRdUserBank::getIsDel, 0);
    }

    private LambdaUpdateWrapper<DtrdEntRdUserBank> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdEntRdUserBank.class)
                        .eq(DtrdEntRdUserBank::getIsDel, 0);
    }
}
